package com.example.demo;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.json.Location;

public class MainActivity extends AppCompatActivity {
    private static final int GET_TOKEN = 1001;
    private static final int GET_FACE_LOCATION = 1002;
    private Button btnGetToken;
    private TextView tvToken;
    private Handler handler;
    private String token;
    private final String TOKEN="24.37b2f1e4b87f1a7e332eca3c160cf714.2592000.1563527490.282335-16561669";
    private Button btnDetect;
    private Location location;
    private MyImageView ivImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getRealMetrics(metric);
        float density = metric.density; // 密度（0.75 / 1.0 / 1.5）

        Log.e("屏幕密度","  "+density);
        ivImage = findViewById(R.id.iv_image);
        btnGetToken=findViewById(R.id.btn_get_token);
        tvToken=findViewById(R.id.tv_token);
        btnDetect=findViewById(R.id.btn_detect);
        btnDetect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String qinFilePath="/mnt/shared/Image/qin.jpg";
                        byte[] qinbytearr=Base64Util.image2byte(qinFilePath);
                        String qinBase64=Base64Util.encode(qinbytearr);
                        Log.e("BASE64",qinBase64);
                        location = FaceDetect.detect(qinBase64);
                        handler.sendEmptyMessage(GET_FACE_LOCATION);
//                        FaceDetect.detect();
                    }
                }).start();
            }
        });
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if(msg.what == GET_TOKEN){
                    tvToken.setText(token);
                    return true;
                }
                if(msg.what ==  GET_FACE_LOCATION){
                    ivImage.showFace(location);
                    return true;
                }
                return false;
            }
        });
        btnGetToken.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        token=AuthService.getAuth();
                        handler.sendEmptyMessage(GET_TOKEN);
                    }
                }).start();
            }
        });
    }
}
